# 不同填充策略
df_stock['ForwardFill'] = df_stock['Price'].fillna(method='ffill')
df_stock['LinearInterp'] = df_stock['Price'].interpolate(method='linear')
# 计算日收益率
df_stock['Return_Normal'] = df_stock['Price_Normal'].pct_change()
df_stock['Return_FF'] = df_stock['ForwardFill'].pct_change()
df_stock['Return_Lin'] = df_stock['LinearInterp'].pct_change()
fig, axes = plt.subplots(2, 1, figsize=(14, 10))
# 价格对比
axes[0].plot(df_stock['Date'], df_stock['Price_Normal'], 'g-',
label='真实价格', linewidth=2)
axes[0].plot(df_stock['Date'], df_stock['ForwardFill'], 'b--',
label='前向填充', linewidth=1.5)
axes[0].plot(df_stock['Date'], df_stock['LinearInterp'], 'r--',
label='线性插值', linewidth=1.5, alpha=0.7)
axes[0].scatter(df_stock[df_stock['Price'].isnull()]['Date'],
df_stock['Price_Normal'][df_stock['Price'].isnull()],
color='red', s=50, zorder=5, label='停牌期间真实价格')
axes[0].set_ylabel('价格', fontsize=12)
axes[0].set_title('股票停牌数据处理:价格对比', fontsize=14)
axes[0].legend(fontsize=11)
axes[0].grid(True, alpha=0.3)
# 收益率对比
axes[1].plot(df_stock['Date'], df_stock['Return_Normal'], 'g-',
label='真实收益率', linewidth=2)
axes[1].plot(df_stock['Date'], df_stock['Return_FF'], 'b--',
label='前向填充收益率', linewidth=1.5)
axes[1].plot(df_stock['Date'], df_stock['Return_Lin'], 'r--',
label='线性插值收益率', linewidth=1.5, alpha=0.7)
axes[1].axhline(y=0, color='k', linestyle='-', linewidth=0.5)
axes[1].set_xlabel('日期', fontsize=12)
axes[1].set_ylabel('日收益率', fontsize=12)
axes[1].set_title('股票停牌数据处理:收益率对比', fontsize=14)
axes[1].legend(fontsize=11)
axes[1].grid(True, alpha=0.3)
plt.tight_layout()
plt.show()